package com.yohann.educms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yohann.commonutils.result.R;
import com.yohann.educms.entity.CrmBanner;
import com.yohann.educms.service.CrmBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 首页banner表 控制器
 * </p>
 *
 * @author Yohann
 * @since 2020-08-11
 */
@RestController
@RequestMapping("/cms-service/banner")
public class BannerAdminController {
    @Autowired
    private CrmBannerService bannerService;

    /**
     * 获取所有banner
     * @return
     */
    @Cacheable(value = "bannerList", key = "'findAllBanner'")
    @GetMapping("findAll")
    public R findAllBanner(){
        QueryWrapper<CrmBanner> bannerQueryWrapper = new QueryWrapper<>();
        bannerQueryWrapper.orderByDesc("id");
        bannerQueryWrapper.last("limit 2");

        return R.ok().data("list",bannerService.list(bannerQueryWrapper));
    }

    /**
     * 分页查询banner
     * @param current
     * @param limit
     * @return
     */
    @PostMapping("pageQuery/{current}/{limit}")
    public R pageQueryTeacher(@PathVariable long current, @PathVariable long limit) {
        Page<CrmBanner> pageBanner = new Page<>(current, limit);
        IPage<CrmBanner> page = bannerService.page(pageBanner, null);

        return R.ok().data("rows",page.getRecords()).data("total",page.getTotal());
    }

    /**
     * 添加banner
     * @param banner
     * @return
     */
    @PostMapping("add")
    public R addBanner(@RequestBody CrmBanner banner){
        bannerService.save(banner);

        return R.ok();
    }

    /**
     * 删除banner
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public R deleteBanner(@PathVariable String id){
        bannerService.removeById(id);

        return R.ok();
    }

    /**
     * 修改banner
     * @param banner
     * @return
     */
    @PostMapping("update")
    public R updateVideo(@RequestBody CrmBanner banner){
        bannerService.updateById(banner);

        return R.ok();
    }
}

